package com.tripreset.sdk.bk

import org.jsoup.Connection
import org.jsoup.Jsoup
import java.net.URLEncoder


interface DataSource {

    fun setUserAgent(userAgent: String)

    fun getDataSourceUrl(keyWord: String): String

    suspend fun loadContent(keyWord: String): ContentInfo

    suspend fun loadContent(keyWord: String, useWeb: Boolean): ContentInfo

}

class BaiDuDataSource : DataSource {

    private var userAgent: String? = null

    override fun setUserAgent(userAgent: String) {
        this.userAgent = userAgent
    }

    override fun getDataSourceUrl(keyWord: String): String {
        return "https://wapbaike.baidu.com/item/${
            URLEncoder.encode(
                keyWord,
                Charsets.UTF_8.toString()
            )
        }"
    }

    override suspend fun loadContent(keyWord: String): ContentInfo {
        val document =
            Jsoup.connect(getDataSourceUrl(keyWord)).timeout(60000).method(Connection.Method.GET)
                .followRedirects(true).get()
        val title = document.title()
        var introduction = ""
        for (element in document.getElementsByTag("meta")) {
            if (element.attr("name") == "description") {
                introduction = element.attr("content")
            }
        }

        //获取 带有src属性的img元素
        /*val imgTags = document.select("img[src]")
        for (element in imgTags) {
            Log.i("sss","========================${element.attr("abs:src")}")
        }*/

        return ContentInfo(title, introduction)
    }

    override suspend fun loadContent(keyWord: String, useWeb: Boolean): ContentInfo = loadContent(keyWord, false)

}

class TouTiaoDataSource : DataSource {
    override fun getDataSourceUrl(keyWord: String): String {
        return "https://m.baike.com/wiki/${URLEncoder.encode(keyWord, Charsets.UTF_8.toString())}"
    }

    override suspend fun loadContent(keyWord: String): ContentInfo {
        val document =
            Jsoup.connect(getDataSourceUrl(keyWord)).timeout(60000).method(Connection.Method.GET)
                .followRedirects(true).get()
        val title = document.title()
        var introduction = ""
        for (element in document.getElementsByTag("meta")) {
            if (element.attr("name") == "description") {
                introduction = element.attr("content")
            }
        }
        //获取 带有src属性的img元素
        /*val imgTags = document.select("img[src]")
        for (element in imgTags) {
            Log.i("sss","========================${element.attr("abs:src")}")
        }*/
        return ContentInfo(title, introduction)
    }

    override suspend fun loadContent(keyWord: String, useWeb: Boolean): ContentInfo = loadContent(keyWord, false)

    private var userAgent: String? = null

    override fun setUserAgent(userAgent: String) {
        this.userAgent = userAgent
    }
}

class SouGouDataSource : DataSource {
    override fun getDataSourceUrl(keyWord: String): String {
        return "https://baike.sogou.com/m/fullLemma?key=${
            URLEncoder.encode(
                keyWord,
                Charsets.UTF_8.toString()
            )
        }&sid=&size=10&g_ut=3 "
    }

    override suspend fun loadContent(keyWord: String): ContentInfo {
        val document =
            Jsoup.connect(getDataSourceUrl(keyWord)).timeout(60000).method(Connection.Method.GET)
                .followRedirects(true).get()
        val title = document.title()
        var introduction = ""
        for (element in document.getElementsByTag("meta")) {
            if (element.attr("name") == "description") {
                introduction = element.attr("content")
            }
        }

        //获取 带有src属性的img元素
        /*val imgTags = document.select("img[src]")
        for (element in imgTags) {
            Log.i("sss","========================${element.attr("abs:src")}")
        }*/
        return ContentInfo(title, introduction)
    }

    override suspend fun loadContent(keyWord: String, useWeb: Boolean): ContentInfo = loadContent(keyWord, false)

    private var userAgent: String? = null

    override fun setUserAgent(userAgent: String) {
        this.userAgent = userAgent
    }
}